<?php # Script 9.5 - register.php #2
$page_title = 'Register';

// Check for form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
	require ('../private/mysqli_connect.php'); // Connect to the db.
		
	$errors = array(); // Initialize an error array.
	
	// Check if the username was entered
	if (empty($_POST['username'])) {
		$errors[] = 'You forgot to enter your username.';
	} else {
		$username = mysqli_real_escape_string($mysqli, trim($_POST['username']));
	}
		
	// Check for a password and match against the confirmed password:
	if (!empty($_POST['pass1'])) {
		if ($_POST['pass1'] != $_POST['pass2']) {
			$errors[] = 'Your password did not match the confirmed password.';
		} else {
			$p = mysqli_real_escape_string($mysqli, trim($_POST['pass1']));
		}
	} else {
		$errors[] = 'You forgot to enter your password.';
	}
	
	if (empty($errors)) { // If everything's OK.
	
		// Register the user in the database...
		
		// Register the user
		$q = "INSERT INTO user_login (u_username, u_pwd) VALUES ('$username', '$p')";		
		$r = @mysqli_query ($mysqli, $q);
		
		if ($r) 
		{ // If it ran OK.
			
			echo '<h1>Thank you!</h1>
				<p>You are now registered. </p><p><br /></p>';
			echo '<p>' . '<a href="login.php">Go Back</a> </p>';
		} 
		else 
		{ // If it did not run OK.
			errorMessageWhenRegisteringUser($mysqli, $q);						
		} // End of if ($r) IF.
		
		mysqli_close($mysqli); // Close the database connection.

		// Include the footer and quit the script:		
		exit();
		
	} else { // Report the errors.
	
		echo '<h1>Error!</h1>
		<p class="error">The following error(s) occurred:<br />';
		
		foreach ($errors as $msg) 
		{ // Print each error.		
			echo " - $msg<br />\n";
		}
		echo '</p><p>Please try again.</p><p><br /></p>';
		echo '<p>' . '<a href="register.php">Go Back</a> </p>';
		
		echo '<script>alert("There was an error during registration.");</script>';
	} // End of if (empty($errors)) IF.
	
	mysqli_close($mysqli); // Close the database connection.

} // End of the main Submit conditional.

function errorMessageWhenRegisteringUser($mysqli, $query)
{
	// Public message:
	echo '<h1>System Error</h1>
	<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; 
	
	// Debugging message:
	echo '<p>' . mysqli_error($mysqli) . '<br /><br />Query: ' . $q . '</p>';
	
	echo '<p>' . '<a href="register.php">Go Back</a> </p>';
}

function testSelectFromDatabase()
{
	// This script performs an INSERT query to add a record to the users table.
	require ('../private/mysqli_connect.php'); // Connect to the db.
	$page_title = 'Register';
	include ('includes/header.html');

	$query = "SELECT u_id FROM `user_login` WHERE u_username = 'mike'";
	$return = @mysqli_query ($mysqli, $query);
	while($row = mysqli_fetch_array($return))
	{
		echo "--" .  $row['u_id'] . "--";
		echo "<br />";
	}
	errorMessageWhenRegisteringUser($mysqli, $query);
}
?>



<!DOCTYPE html>
<html>
<head runat="server">
	<title>
		Register
	</title>
	<link href="style.css" rel="Stylesheet" type="text/css" />
</head>

<body>
	<a name="top"></a>
	<div class="WebsiteBanner">Study Buddy</div>
	<div class="NavigationBar">
		<span id="NavigationBarLink">
			<a href="login.php">Log In</a>
		</span>
		<span id="NavigationBarLink">
			<a href="register.php">Register</a>
		</span>
	</div>
	<div class="PostContainer">	
		<div class="Post">
			<div class="PostHeader">
				Register
			</div>
			<div class="PostContent">
				<h1>Register</h1>
				<form action="register.php" method="post">
					<p>Username: <input type="text" name="username" size="15" maxlength="20" value="<?php if (isset($_POST['username'])) echo $_POST['username']; ?>" /></p>
					<p>Password: <input type="password" name="pass1" size="10" maxlength="20" value="<?php if (isset($_POST['pass1'])) echo $_POST['pass1']; ?>"  /></p>
					<p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" value="<?php if (isset($_POST['pass2'])) echo $_POST['pass2']; ?>"  /></p>
					<p><input type="submit" name="submit" value="Register" /></p>
				</form>
			</div>
		</div>
	</div>
	<div  id="BackToTop">
		<a href="#top">Back to Top</a>
	</div>
</body>
</html>